package com.abel.he.springbooteasyexcelimport.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.abel.he.springbooteasyexcelimport.entity.ImportVo;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 导入类
 *
 * @author heyangyang
 * @since 2024-01-15
 */

@RestController
public class ImportController {

    /**
     * 总条数
     */
    private static final String ROW_COUNT = "ROW_COUNT";

    /**
     * 读取到的行数
     */
    private static final String ROW_INDEX = "ROW_INDEX";

    /**
     * 消息列表
     */
    private static final String MESSAGE_KEY = "MESSAGE";

    private static final Map<String, Object> PROGRESS_BAR_MAP = Maps.newConcurrentMap();

    /**
     * 导入
     *
     * @param file 文件
     * @throws IOException io异常
     */
    @RequestMapping("/import")
    public void importExcel(MultipartFile file) throws IOException {

        EasyExcelFactory.read(file.getInputStream(), ImportVo.class, new ReadListener<ImportVo>() {
            @SneakyThrows
            @Override
            public void invoke(ImportVo importVo, AnalysisContext analysisContext) {
                Integer rowCount = analysisContext.readSheetHolder().getApproximateTotalRowNumber() - 1;
                // 打印数据
                System.out.println(importVo);
                // 更新进度条
                int randowm = RandomUtil.randomInt(10);
                System.out.println("随机数：" + randowm);
                if (randowm % 2 == 0) {
                    progressBarHandle(rowCount);
                } else {
                    progressBarHandle(rowCount, Lists.newArrayList(
                            "第" + analysisContext.readSheetHolder().getRowIndex() + "数据有误",
                            "第" + analysisContext.readSheetHolder().getRowIndex() + "数据有误"));
                }

                // 延迟消息
                Thread.sleep(1000);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                removeProgress();
            }
        }).headRowNumber(1).sheet().doRead();
    }

    /**
     * 获取进度条
     */
    @GetMapping("/progress/bar")
    public String importExcel2() {
        return PROGRESS_BAR_MAP.toString();
    }

    /**
     * 移除进度条
     */
    private void removeProgress() {
        PROGRESS_BAR_MAP.remove(ROW_COUNT);
        PROGRESS_BAR_MAP.remove(ROW_INDEX);
    }

    /**
     * 更新季度条数据
     *
     * @param rowCount 数量
     */
    private void progressBarHandle(Integer rowCount) {
        progressBarHandle(rowCount, Lists.newArrayList());

    }

    /**
     * 更新季度条数据
     *
     * @param rowCount 数量
     */
    private void progressBarHandle(Integer rowCount, List<String> messages) {
        Integer integer = (Integer) PROGRESS_BAR_MAP.get(ROW_INDEX);
        if (Objects.isNull(integer)) {
            integer = 0;
        }
        integer++;
        PROGRESS_BAR_MAP.put(ROW_COUNT, rowCount);
        PROGRESS_BAR_MAP.put(ROW_INDEX, integer);

        List<String> messageList;
        if (CollUtil.isNotEmpty(messages)) {
            messageList = (List<String>) PROGRESS_BAR_MAP.get(MESSAGE_KEY);
            if (Objects.isNull(messageList)) {
                messageList = Lists.newArrayList();
            }
            messageList.addAll(messages);
            PROGRESS_BAR_MAP.put(MESSAGE_KEY, messageList);
        }
    }

}
